$(function () {
    obj.init();
});

var obj = new function () {
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Device/getExchangeDevice');
            this.dataGrid.parseToolbar().init().activeButton(['bt_list_check', 'bt_list_financeCheck']);
            this.funObj().bindDataGridEvent();
            this.listener();
        },
        listener: function () {
            var $this = this;
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().searchList();
                })
            );
            //审核
            $('#bt_list_check').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().check(1);
            });
            //财务审核
            $('#bt_list_financeCheck').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().check(2);
            });
        },
        funObj: function () {
            var $this = this;
            return {
                bindDataGridEvent: function () {
                    $this.dataGrid.onLoadSuccess = function (data) {
                        if (xhExtra.checkError(data)) return false;
                        if (data.rows.length <= 0) return false;
                        $this.dataGrid.mergeCells(['sDevNumber', 'sMacAddress', 'sNewReceiver']);
                    };
                },
                searchList: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        applyStatus: xhExtra.getValue($('#list_applyStatus')),
                        financeStatus: xhExtra.getValue($('#list_financeStatus'))
                    })
                },
                check: function (_type) {
                    var iId = $this.dataGrid.getSelectValues('iId');
                    var iCheckStatus = $this.dataGrid.getSelectValues('iCheckStatus');
                    var iFinanceStatus = $this.dataGrid.getSelectValues('iFinanceStatus');
                    if (iId.length < 1) {
                        window.top.xhTop.showError('请选择后操作');
                        return false;
                    }
                    if (_type === 1) {//申请审核
                        var title = '设置申请审核结果';
                        var checkTrue = false;
                        iCheckStatus.forEach(function(name) {
                            if (name.indexOf("未审核") === -1) {
                                checkTrue = true;
                            }
                        });
                        iFinanceStatus.forEach(function(name) {
                            if (name.indexOf("未审核") === -1) {
                                checkTrue = true;
                            }
                        });
                        if (checkTrue) {
                            window.top.xhTop.showError('存在不允许操作的申请，请重新选择');
                            return false;
                        }
                    } else {//财务审核
                        title = '设置财务审核结果';
                        checkTrue = false;
                        iCheckStatus.forEach(function(name) {
                            if (name.indexOf("未审核") !== -1 || name.indexOf("不通过") !== -1) {
                                checkTrue = true;
                            }
                        });
                        iFinanceStatus.forEach(function(name) {
                            if (name.indexOf("未审核") === -1) {
                                checkTrue = true;
                            }
                        });
                        if (checkTrue) {
                            window.top.xhTop.showError('存在不允许操作的申请，请重新选择');
                            return false;
                        }
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: title,
                        html: '<div>' +
                        '<label>' +
                        '设置所选'+ iId.length +'条申请的审核结果' +
                        '</label>' +
                        '<select id="status" class="swal2-input">' +
                        '<option value="1">通过</option>' +
                        '<option value="2">不通过</option>' +
                        '</select>' +
                        '</div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var status = $('#status', window.top.document).val();
                                xhExtra.ajaxRequest('Device/saveExchangeStatus', {
                                    ids: iId,
                                    status: status,
                                    type: _type
                                }, {success: true});
                                resolve();
                                $this.dataGrid.reload();
                            })
                        }
                    })
                }
            }
        }
    }
};